var app = getApp();

Page({
  data: {
    orderId: ''
  },
  onLoad: function (query) {
    app.getUrl(this)
    app.getSystem(this)
    this.initData(query.id)
    this.getOrderInfo(query.id)
  },
  /**
   * @function 下拉刷新回调
  */
  onPullDownRefresh () {
    console.log('下拉刷新')
    this.getOrderInfo(this.data.orderId, () => {
      //停止下拉刷新
      wx.stopPullDownRefresh()
    })
  },
  /**
   * @function 初始化参数
   * @param {*} orderId 订单ID
   */
  initData (orderId) {
    this.setData({
      orderId
    })
  },
  /**
   * @function 获取订单详情
   * @param {*} cb 成功后的回调函数，之前没有做promise封装，所以用cb
   */
  getOrderInfo (id, cb) {
    app.util.request({
      url: "order/info",
      data: {
        id
      },
      testapi: app.siteInfo.siteroot_admin,
      success: (res) => {
        var e = res.data.data;
        e.arrival_time = e.arrival_time.slice(5, 7) + "月" + e.arrival_time.slice(8, 10) + "日",
          e.departure_time = e.departure_time.slice(5, 7) + "月" + e.departure_time.slice(8, 10) + "日",
          e.time = app.ormatDate(e.time), 20 <= e.seller_address.length && (e.seller_address = e.seller_address.slice(0, 20) + "..."),
          this.setData({
            order_info: e
          });

          cb && cb()
      }
    })
  },
  /**
   * @function 取消入住
   * @param {*} t 
   */
  cancelLive (e) {
    wx.showModal({
      editable: true,
      title:"取消原因",
      placeholderText: '输入取消原因',
      confirmColor: '#ff9900',
      success: (res) => {
        if (res.cancel) {
          console.log(res.content, "用户点击了取消")
        }

        if (res.confirm) {
          var remarks = res.content;
          app.util.request({
            url: "order/confirmCheckin",
            data: {
              id: e.currentTarget.dataset.id,
              confirm_type: e.currentTarget.dataset.status,
              remark:remarks
            },
            testapi: app.siteInfo.siteroot_admin,
            success: (t) => {
              wx.showToast({
                title: "入住已取消"
              })
              this.getOrderInfo(this.data.orderId)
            }
          });
        }
      }
    })
  },
  /**
   * @function 确认入住
   * @param {*} t 
   */
  confirmLive (e) {
    app.util.request({
      url: "order/confirmCheckin",
      data: {
        id: e.currentTarget.dataset.id,
        confirm_type: e.currentTarget.dataset.status
      },
      testapi: app.siteInfo.siteroot_admin,
      success: (t) => {
        wx.showToast({
          title: "入住已成功"
        })
        this.getOrderInfo(this.data.orderId)
      }
    })
  },
  /**
   * @function 取消订单
   * @param {*} t 
   */
  cancelOrder (e) {
    wx.showModal({
      editable: true,
      title:"拒绝理由",
      placeholderText: '输入拒绝理由',
      confirmColor: '#ff9900',
      success: (res) => {
        if (res.cancel) {
          console.log(res.content, "用户点击了取消")
        }

        if (res.confirm) {
          var remarks = res.content;
          app.util.request({
            url: "order/confirmOrder",
            data: {
              id: e.currentTarget.dataset.id,
              confirm_type: e.currentTarget.dataset.status,
              remark:remarks
            },
            testapi: app.siteInfo.siteroot_admin,
            success: (t) => {
              wx.showToast({
                title: "订单已拒绝"
              })
              this.getOrderInfo(this.data.orderId)
            }
          });
        }
      }
    })
  },
  /**
   * @function 确认订单
   * @param {*} t 
   */
  confirmOrder (e) {
    app.util.request({
      url: "order/confirmOrder",
      data: {
        id: e.currentTarget.dataset.id,
        confirm_type: e.currentTarget.dataset.status,
      },
      testapi: app.siteInfo.siteroot_admin,
      success: (t) => {
        wx.showToast({
          title: "订单已确认"
        })
        this.getOrderInfo(this.data.orderId)
      }
    });
  },
  /**
   * @function 已入住状态下，申请退款操作
   */
  refundOrder (e) {
    wx.showModal({
      editable: true,
      title:"退款理由",
      placeholderText: '输入退款理由',
      confirmColor: '#ff9900',
      success: (res) => {
        if (res.confirm) {
          var remarks = res.content;
          app.util.request({
            url: "order/applyRefund",
            data: {
              id: e.currentTarget.dataset.id,
              remark:remarks
            },
            testapi: app.siteInfo.siteroot_admin,
            success: () => {
              wx.showToast({
                title: "发起退款成功"
              })
              this.getOrderInfo(this.data.orderId)
            }
          });
        }
      }
    })
  },
  /**
   * @function 已入住后，确认完成订单
   */
  finishOrder (e) {
    wx.showModal({
      title:"温馨提示",
      content: '确认完成该订单吗？',
      confirmColor: '#ff9900',
      success: (res) => {
        if (res.confirm) {
          app.util.request({
            url: "order/finishOrder",
            data: {
              id: e.currentTarget.dataset.id
            },
            testapi: app.siteInfo.siteroot_admin,
            success: (t) => {
              wx.showToast({
                title: "完成订单成功"
              })
              this.getOrderInfo(this.data.orderId)
            }
          })
        }
      }
    })
  }
});